package com.galaxy.service.controller.system;

import com.galaxy.commom.base.annotation.WebLog;
import com.galaxy.commom.base.message.ResponseCode;
import com.galaxy.commom.base.message.ResponseResult;
import com.galaxy.service.dto.system.user.QueryUserDto;
import com.galaxy.service.dto.system.user.ResetPasswordDto;
import com.galaxy.service.dto.system.user.UserAddDto;
import com.galaxy.service.dto.system.user.UserUpdateDto;
import com.galaxy.service.service.system.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户控制器
 * @author caijinbang
 * @date 2018/10/28 00:01
 */
@Api(tags = {"用户管理接口"})
@RestController
@RequestMapping(value = "/system/user")
public class UserController {

  @Autowired
  private UserService userService;

  @WebLog("分页获取用户数据")
  @PostMapping(value = {"/list"})
  @ApiOperation(value = "分页获取用户数据")
  @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
      public ResponseResult get(@RequestBody @Validated @ApiParam(value = "用户获取过滤条件") QueryUserDto queryUserDto){
    return ResponseResult.e(ResponseCode.OK,userService.getAllUserBySplitPage(queryUserDto));
  }

  @WebLog("根据ID获取用户信息")
  @PostMapping(value = {"/get/id/{id}"})
  @ApiOperation(value = "根据ID获取用户信息")
  public ResponseResult getById(@PathVariable("id") @ApiParam(value = "用户ID") String id){
    return ResponseResult.e(ResponseCode.OK,userService.findUserById(id,true));
  }

  @PostMapping(value = {"/lock/{id}"})
  @ApiOperation(value = "锁定用户")
  @WebLog("锁定用户")
  @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
  public ResponseResult lock(@PathVariable("id") @ApiParam(value = "用户标识ID") String id){
    userService.statusChange(id, 0);
    return ResponseResult.e(ResponseCode.OK);
  }

  @PostMapping(value = {"/unlock/{id}"})
  @ApiOperation(value = "解锁用户")
  @WebLog("解锁用户")
  @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
  public ResponseResult unlock(@PathVariable("id") @ApiParam(value = "用户标识ID") String id){
    userService.statusChange(id, 1);
    return ResponseResult.e(ResponseCode.OK);
  }

  @PostMapping(value = {"/remove/{id}"})
  @ApiOperation(value = "删除用户")
  @WebLog("删除用户")
  public ResponseResult remove(@PathVariable("id") @ApiParam(value = "用户标识ID") String id){
    userService.removeUser(id);
    return ResponseResult.e(ResponseCode.OK);
  }

  @PostMapping(value = {"/add"})
  @ApiOperation(value = "添加用户")
  @WebLog("添加用户")
  @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
  public ResponseResult add(@RequestBody @Validated @ApiParam(value = "用户数据") UserAddDto userAddDto){
    userService.add(userAddDto);
    return ResponseResult.e(ResponseCode.OK);
  }

  @PostMapping(value = {"/update/{id}"})
  @ApiOperation(value = "更新用户")
  @WebLog("更新用户")
  @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
  public ResponseResult update(@PathVariable("id") @ApiParam(value = "用户标识ID") String id,
      @RequestBody @Validated @ApiParam(value = "用户数据") UserUpdateDto userUpdateDto){
    userService.update(id,userUpdateDto);
    return ResponseResult.e(ResponseCode.OK);
  }

  @PostMapping(value = {"/reset-password"})
  @ApiOperation(value = "重置密码")
  @WebLog("重置密码")
  @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
  public ResponseResult resetPassword(@RequestBody
  @Validated @ApiParam(value = "用户及密码数据") ResetPasswordDto dto){
    userService.resetPassword(dto);
    return ResponseResult.e(ResponseCode.OK);
  }


}
